
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :chengbiao.he
	CreateTime:2008-02-22 13:52:54
*/
public class PositionSrv extends IHandler
{

	public PositionSrv() 
	{
		super();
	}
	private void deletePosition(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		Position obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Position.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void savePosition(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		Position obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = Position.fetch();
    	}
    	else                     //update
    	{
    		obj = Position.fetch(con,id);
    	}
		PositionPageData data = (PositionPageData)PageData.fromForm(context,PositionPageData.class);
		context.setValue("PositionPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listPosition(DBConnection con,SrvContext context,String path) throws Exception
	{
		PositionSearchEvent event = (PositionSearchEvent)SearchEvent.fromForm(context,PositionSearchEvent.class);
		context.setSessionValue("PositionSearchEvent",event);
		context.setNextUrl(path+"PositionList.jsp");
	}
	private void backPosition(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"PositionList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listPosition"))
		{
			listPosition(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewPosition"))
		{
			String id = context.getString("Id",null);
			Position obj = Position.fetch(con,id);
			context.setValue("Position",obj);
			context.setNextUrl(path+"PositionView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deletePosition"))
		{
			context.setErrorUrl(path+"PositionList.jsp");
			deletePosition(con,context);
			backPosition(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddPosition"))
		{
			Position obj = Position.fetch();
			PositionPageData data = (PositionPageData)PageData.fromTable(obj,PositionPageData.class);
			context.setValue("PositionPageData",data);
			context.setNextUrl(path+"PositionEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdatePosition"))
		{
			String id = context.getString("Id",null);
			Position obj = Position.fetch(con,id);
			PositionPageData data = (PositionPageData)PageData.fromTable(obj,PositionPageData.class);
			context.setValue("PositionPageData",data);
			context.setNextUrl(path+"PositionEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyPosition"))
		{
			String id = context.getString("Id",null);
			Position obj = Position.copy(Position.fetch(con,id));
			PositionPageData data = (PositionPageData)PageData.fromTable(obj,PositionPageData.class);
			context.setValue("PositionPageData",data);
			context.setNextUrl(path+"PositionEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("savePosition"))
		{
			context.setErrorUrl(path+"PositionEdit.jsp");
			savePosition(con,context,loginContext);
			backPosition(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewPosition"))
		{
			context.setErrorUrl(path+"PositionEdit.jsp");
			savePosition(con,context,loginContext);
			Position obj = Position.fetch();
			PositionPageData data = (PositionPageData)PageData.fromTable(obj,PositionPageData.class);
			context.setValue("PositionPageData",data);
			context.setNextUrl(path+"PositionEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListPosition"))
		{
			backPosition(con,context,path);
			return;
		}
		
		//add by chengbiao.he start

		//add by chengbiao.he end

		Logger.throwException("PositionSrv","unknown action_code="+action_code);
	}
}
